﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using EOS.DataModel;
using EOS.IBLL;
using EOS.Common;
using EOS.BLL;

namespace EOS.EntWebDemo.Controllers
{
    public class LoginController : Controller
    {
        //实例化UserInfo接口的对象
        IUserInfoService _iUserInfoService = new UserInfoService();


        public ActionResult Index()
        {
            //获取到Cookie中的值传递给前台显示
            var UName = Request.Cookies["UName"] == null ? "" : Request.Cookies["UName"].Value;
            ViewBag.UName = UName;
            return View();
        }

        /// <summary>
        /// 处理登录的信息
        /// </summary>
        /// <param name="userInfo"></param>
        /// <param name="Code"></param>
        /// <returns></returns>
        public ActionResult CheckUserLogin(UserInfo userInfo, string Code)
        {
            //如果有用户名的话讲用户名存放到Cookie中
            if (userInfo.UName != null)
            {
                Response.Cookies["UName"].Value = userInfo.UName;
                Response.Cookies["UName"].Expires = DateTime.Now.AddDays(7);
            }


            //这里验证用户输入的验证码和系统的验证码是否相同
            string sessionCode = Session["ValidateCode"] == null ? new Guid().ToString() : Session["ValidateCode"].ToString();

            //将验证码去掉，避免暴力破解
            Session["ValidateCode"] = new Guid();

            if (sessionCode != Code)
            {
                return Content("请输入正确的验证码");
            }

            //调用BLL检验用户名密码是否正确
            UserInfo uinfo = _iUserInfoService.checkUserLogin(userInfo);
            if (uinfo != null)
            {
                //提供Session接口方便后面判断用户登录
                Session["UserInfo"] = uinfo;
                return Content("OK");
            }
            else
            {
                return Content("用户名密码错误，请您检查");
            }
        }

        /// <summary>
        /// 验证码的校验
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public ActionResult CheckCode()
        {
            //生成验证码
            ValidateCode validateCode = new ValidateCode();
            string code = validateCode.CreateValidateCode(4);
            Session["ValidateCode"] = code;
            byte[] bytes = validateCode.CreateValidateGraphic(code);
            return File(bytes, @"image/jpeg");
        }
        //private DataModelContainer db = new DataModelContainer();

        //// GET: Login
        //public ActionResult Index()
        //{
        //    return View(db.UserInfo.ToList());
        //}

        //// GET: Login/Details/5
        //public ActionResult Details(int? id)
        //{
        //    if (id == null)
        //    {
        //        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        //    }
        //    UserInfo userInfo = db.UserInfo.Find(id);
        //    if (userInfo == null)
        //    {
        //        return HttpNotFound();
        //    }
        //    return View(userInfo);
        //}

        //// GET: Login/Create
        //public ActionResult Create()
        //{
        //    return View();
        //}

        //// POST: Login/Create
        //// 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        //// 详细信息，请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
        //[HttpPost]
        //[ValidateAntiForgeryToken]
        //public ActionResult Create([Bind(Include = "ID,UName,Pwd,Phone,Mail,SubTime,LastModifiedOn,DelFlag")] UserInfo userInfo)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        db.UserInfo.Add(userInfo);  
        //        db.SaveChanges();
        //        return RedirectToAction("Index");
        //    }

        //    return View(userInfo);
        //}

        //// GET: Login/Edit/5
        //public ActionResult Edit(int? id)
        //{
        //    if (id == null)
        //    {
        //        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        //    }
        //    UserInfo userInfo = db.UserInfo.Find(id);
        //    if (userInfo == null)
        //    {
        //        return HttpNotFound();
        //    }
        //    return View(userInfo);
        //}

        //// POST: Login/Edit/5
        //// 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        //// 详细信息，请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
        //[HttpPost]
        //[ValidateAntiForgeryToken]
        //public ActionResult Edit([Bind(Include = "ID,UName,Pwd,Phone,Mail,SubTime,LastModifiedOn,DelFlag")] UserInfo userInfo)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        db.Entry(userInfo).State = EntityState.Modified;
        //        db.SaveChanges();
        //        return RedirectToAction("Index");
        //    }
        //    return View(userInfo);
        //}

        //// GET: Login/Delete/5
        //public ActionResult Delete(int? id)
        //{
        //    if (id == null)
        //    {
        //        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        //    }
        //    UserInfo userInfo = db.UserInfo.Find(id);
        //    if (userInfo == null)
        //    {
        //        return HttpNotFound();
        //    }
        //    return View(userInfo);
        //}

        //// POST: Login/Delete/5
        //[HttpPost, ActionName("Delete")]
        //[ValidateAntiForgeryToken]
        //public ActionResult DeleteConfirmed(int id)
        //{
        //    UserInfo userInfo = db.UserInfo.Find(id);
        //    db.UserInfo.Remove(userInfo);
        //    db.SaveChanges();
        //    return RedirectToAction("Index");
        //}

        //protected override void Dispose(bool disposing)
        //{
        //    if (disposing)
        //    {
        //        db.Dispose();
        //    }
        //    base.Dispose(disposing);
        //}
    }
}
